/**
 * @file
 * @auth jinguangguo
 * @date 2017/01/19

 确认卖单的  对话框

 */

import Modal from '../../../grsc-base/component/Modal.vue';
import Validator from '../../../grsc-base/component/Validator/main';
import {PLACEHOLDER_ENTER_ASSET_PASSWORD} from '../../common/contants';
import tpl from './PayDialog.html';
import service from './service';
import util from './util';
import commonUtil from '../../common/util'
import format from '../../common/format'
import filter from '../../../grsc-base/util/filter';

export default Vue.extend({
    name: "BusinessLimitPay",
    template: tpl,
    components: {
        Modal,
        Validator
    },
    data() {
        return {
            isBuy: false,
            precision: 0,
            price: 0,
            amount: 0,
            form: {
                paypwd: ''
            },
            rules: {
                paypwd: {
                    required: true,
                    requiredTip: this.$t('common.assetPassRequireTip')
                }
            },
            isRequesting: false
        };
    },
    props: {
        reqParam: {
            type: Object,
            deafult: {}
        },
        onSuccess: {
            type: Function,
            default() {

            }
        }
    },
    created(){
        this.precision = this.$$coinPrecision;
        this.price = filter.floorFix(this.reqParam.price, this.precision) + ' ' + this.$$tradeUnit;
        this.amount = this.reqParam.amount + ' ' + this.$$tradeType;
    },
    mounted() {
        $(this.$refs.input).focus();
    },
    methods: {
        submit(){
            if (this.rules.$checkAll() === false) {
                return;
            }

            this.isRequesting = true;

            let param = $.extend({}, this.reqParam, {
                orderNo:commonUtil.getUuid(),
                symbol:this.$$symbol
            });
            let header = {
                'pay-password':this.form.paypwd
            };

            service.order(param,header).done(rep => {
                if (rep.code === window.CODE.SUCCESS) {
                    this.hide();
                    // $(document).trigger('$$assets.OrderList');
                    window.success(this.$t('payDialog.successOrder'), () => {
                        this.onSuccess();
                        $(document).trigger('$$assets.OrderList');
                        // 提交之后，需要刷盘
                        // $(document).trigger('$$pan');
                    });
                } else if (rep.code === window.CODE.PAY_PWD.CODE && rep.data && rep.data.num) {
                    let num = rep.data.num;
                    if (3 - num > 0) {
                        this.rules.paypwd.$setError(this.$t('common.IncorrectAssetPassTip',{num:(3-num)}));
                    } else {
                        this.rules.paypwd.$setError(window.CODE.PAY_PWD_LOCK[this.$i18n.locale]);
                    }
                } else if (rep.code === window.CODE.PAY_PWD_LOCK.CODE) {
                    this.rules.paypwd.$setError(window.CODE.PAY_PWD_LOCK[this.$i18n.locale]);
                } else {
                    this.hide();
                    window.error(rep.msg);
                    //util.doPayPassError(rep, this.rules);
                }

            }).always(() => {
                this.isRequesting = false;
            });
        },
        hide() {
            this.$emit('hide');
        }
    }


});